<?php

namespace app\common\job;

use app\common\constant\driving\CommonConstant;
use app\common\model\driving\DriverRechargeOrder;
use app\common\service\driver\OrderReceiveService;
use think\Exception;
use think\queue\Job;

class OrderJob
{

    /**
     * 三方渠道订单分配司机
     * @param Job $job
     * @param $data
     */
    public function DistributeOrderRun(Job $job, $data)
    {
        try {
            $order_id = !empty($data['order_id']) ? $data['order_id'] : '';
            OrderReceiveService::distribute_order($order_id);
            $job->delete();
        } catch (Exception $e) {
            $job->delete();
            user_log('job_order', [
                'msg' => '三方渠道订单分配司机错误',
                'error' => $e->getMessage(),
                'data' => $data,
            ]);
        }
    }

    /**
     * 司机充值订单自动过期
     * @param Job $job
     * @param $data
     */
    public function RechargeOrderExpiredRun(Job $job, $data){
        try {
            $order_id = !empty($data['order_id']) ? $data['order_id'] : '';
            $order = DriverRechargeOrder::field('id,status')->find($order_id);
            if ($order && $order['status'] == CommonConstant::STATUS_CREATED) {
                $order->status = CommonConstant::STATUS_EXPIRED;
                $order->save();
            }
            $job->delete();
        } catch (Exception $e) {
            $job->delete();
            user_log('job_order', [
                'msg' => '司机充值订单自动过期错误',
                'error' => $e->getMessage(),
                'data' => $data,
            ]);
        }
    }

}